package com.condroid;

import ioio.lib.api.Uart;

/**
 * this class content the parameter information
 * of Uart. 
 * @author JUN GAO
 * Condroid Summer CSD 2011 - KTH
 */
public class UartInfo {
	private int rxPin;
	private int txPin;
	private int baud;
	private Uart.Parity parity;
	private Uart.StopBits stopBits;
	
	public UartInfo(){}
	
	/**
	 * @param rxPin the pin number used for receive
	 * @param txPin the pin number used for transmit
	 * @param baud the baud rate
	 * @param parity the parity
	 * @param stopBits the stop bits
	 */
	public UartInfo(int rxPin, int txPin, int baud, Uart.Parity parity, Uart.StopBits stopBits){
		this.rxPin = rxPin;
		this.txPin = txPin;
		this.baud = baud;
		this.parity = parity;
		this.stopBits = stopBits;
	}
	
	/**
	 * use to get the receiving pin number
	 * @return pin number used to receive
	 */
	public int getRxPin(){
		return rxPin;
	}
	
	/**
	 * use to get the transmit pin number
	 * @return pin number used to transmit
	 */
	public int getTxPin(){
		return txPin;
	}
	
	/**
	 * used to get the baud rate
	 * @return the baud rate
	 */
	public int getBaud(){
		return baud;
	}
	
	/**
	 * used to get the parity
	 * @return the parity
	 */
	public Uart.Parity getParity(){
		return parity;
	}
	
	/**
	 * used to get the stop bits
	 * @return the stop bits
	 */
	public Uart.StopBits getStopBits(){
		return stopBits;
	}
	
	/**
	 * used to set the receive pin number
	 * @param rxPin receive pin number
	 */
	public void setRxPin(int rxPin){
		this.rxPin = rxPin;
	}
	
	/**
	 * used to set the transmit pin number
	 * @param txPin transmit pin number
	 */
	public void setTxPin(int txPin){
		this.txPin = txPin;
	}
	
	/**
	 * used to set the baud rate
	 * @param baud the baud rate
	 */
	public void setBaud(int baud){
		this.baud = baud;
	}
	
	/**
	 * used to set the parity
	 * @param parity the parity
	 */
	public void setParity(Uart.Parity parity){
		this.parity = parity;
	}
	
	/**
	 * used to set the stop bits
	 * @param stopBits the stop bits
	 */
	public void setStopBits(Uart.StopBits stopBits){
		this.stopBits = stopBits;
	}
	
	/**
	 * used to check if there conflict pin using
	 * @param info UartInfo used to check with this instant.
	 * @return true if there is conflict.
	 */
	public boolean hasSamePin(UartInfo info) {
		return rxPin == info.getRxPin() || rxPin == info.getTxPin()
				|| txPin == info.getRxPin() || txPin == info.getTxPin();
	}
	
	/**
	 * used to check if the parameter is OK
	 * @return true if OK or will be false.
	 */
	public boolean isInfoOk(){
		if(rxPin <= 0)
			return false;
		
		if(txPin <= 0)
			return false;
		
		if(parity == null)
			return false;
		
		if(stopBits == null)
			return false;
		
		if(!isBaudOk(baud))
			return false;
		
		return true;
	}
	
	/**
	 * only check the baud rate is OK or not
	 * @param baud the value of baud rate 
	 * @return true if the baud rate is usable or will be false
	 */
	public static boolean isBaudOk(int baud){
		switch(baud){
		case 110:
			break;
		case 300:
			break;
		case 600:
			break;
		case 1200:
			break;
		case 2400:
			break;
		case 4800:
			break;
		case 9600:
			break;
		case 14400:
			break;
		case 19200:
			break;
		case 38400:
			break;
		case 56000:
			break;
		case 57600:
			break;
		case 115200:
			break;
		case 128000:
			break;
		case 256000:
			break;
		default:
			return false;
		}
		
		return true;
	}

	/**
	 * compare if the parameter in UartInfo is the same with current one.
	 */
	@Override
	public boolean equals(Object object) {
		UartInfo u = (UartInfo) object;
		return rxPin == u.getRxPin() && txPin == u.getTxPin() && baud == u.getBaud()
				&& parity == u.getParity() && stopBits == u.getStopBits();
	}
	
	/**
	 * return the parameter contented by this instant in a string.
	 */
	@Override
	public String toString() {
		String parity, stopBits;

		if (this.parity == Uart.Parity.EVEN)
			parity = "even";
		else if (this.parity == Uart.Parity.NONE)
			parity = "none";
		else if (this.parity == Uart.Parity.ODD)
			parity = "odd";
		else
			parity = "unknown";

		if (this.stopBits == Uart.StopBits.ONE)
			stopBits = "one";
		else if (this.stopBits == Uart.StopBits.TWO)
			stopBits = "two";
		else
			stopBits = "unknown";

		return "Rx: " + rxPin + " Tx: " + txPin + " Baud rate: " + baud
				+ " Parity: " + parity + " Stop bits: " + stopBits;
	}
}
